﻿using System;
using System.Text;
using System.IO;
using iTextSharp.text.pdf;
using iTextSharp.text.pdf.parser;
using iTextSharp.text;

namespace PdfToText
{
    /// <summary>
    /// The main entry point to the program.
    /// </summary>
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                if (args.Length < 1)
                {
                    DisplayUsage();
                    Console.ReadKey();
                    return;
                }

                string file = args[0];
                if (!File.Exists(file))
                {
                    file = Path.GetFullPath(file);
                    if (!File.Exists(file))
                    {
                        Console.WriteLine("Please give in the path to the PDF file.");
                    }
                }
                PdfReader reader = new PdfReader(file);
                string text = PdfTextExtractor.GetTextFromPage(reader, 1);
                Console.WriteLine(text);

                FncWriteText("    1    2 3 11 12    13 1 2 3 11 12 13 1 2 3 11 12 13 1 2 3 11 12 13", "1");
                //PDFParser pdfParser = new PDFParser();
                //pdfParser.ExtractText(file, Path.GetFileNameWithoutExtension(file) + ".txt");
            }
            catch (Exception exc)
            {
                Console.WriteLine(exc);
            }
            Console.ReadKey();
        }

        static void DisplayUsage()
        {
            Console.WriteLine();
            Console.WriteLine("Usage:\tpdftotext FILE");
            Console.WriteLine();
            Console.WriteLine("\tFILE\t the path to the PDF file, it may be relative or absolute.");
            Console.WriteLine();
        }

        static void FncWriteText(string pLine, string pMatch)
        {
            if (pLine.Length == 0)
                return;
            Document oDocument = new Document();
            try
            {
                PdfWriter oPdfWriter = PdfWriter.GetInstance(oDocument, new FileStream("TextTest.pdf", FileMode.Create));
                oDocument.Open();
                //PdfContentByte oPdfContentByte = oPdfWriter.DirectContent;
                PdfSpotColor psc_cmyk = new PdfSpotColor("iTextSpotColorCMYK", new CMYKColor(0.3f, .9f, .3f, .1f));
                SpotColor sc_cmyk = new SpotColor(psc_cmyk, 0.5f);
                Font oHigltLineFont = new Font(Font.NORMAL, 24, Font.BOLD, sc_cmyk);
                Font oNormalFont = new Font(Font.NORMAL, 24);
                int i = 0;
                string sResult = string.Empty;
                while (i < pLine.Length)
                {
                    if (char.IsWhiteSpace(pLine[i]))
                    {
                        FncWriteText(oDocument, sResult, pMatch, oNormalFont, oHigltLineFont);
                        sResult = string.Empty;
                        oDocument.Add(new Phrase(" ", new Font(Font.NORMAL, 24)));
                    }
                    else
                    {
                        sResult += pLine[i].ToString();
                    }
                    i++;
                }
                FncWriteText(oDocument, sResult, pMatch, oNormalFont, oHigltLineFont);
            }
            catch (DocumentException de)
            {
                Console.WriteLine(de.Message);
            }
            catch (IOException ioe)
            {
                Console.WriteLine(ioe.Message);
            }
            oDocument.Close();
        }

        static void FncWriteText(Document pDocument, string pText, string pMatch, Font pNormalFont, Font pHigltLineFont)
        {
            if (!string.IsNullOrEmpty(pText))
            {
                if (string.Compare(pText, pMatch, StringComparison.CurrentCulture) == 0)
                    pDocument.Add(new Phrase(pText, pHigltLineFont));
                else
                    pDocument.Add(new Phrase(pText, pNormalFont));
            }
        }
    }
}
